<template>
  <div class="tags" v-if="showTags" ref="tags">
    <div class="tags-tab" ref="outBox">
      <ul
        ref="box"
        :style="{
          'padding-left': arrowVisible ? '30px' : '0',
          left: `${left}px`,
        }"
      >
        <li
          class="tags-li"
          v-for="(item, index) in tagsList"
          :class="{ active: isActive(item.path) }"
          :key="index"
        >
          <router-link :to="item.name=='welldata' ? {name:item.name, params: {menuName:item.menuName}} : item.path" class="tags-li-title">
            {{ item.title }}
          </router-link>
          <span class="tags-li-icon" @click="closeTags(index)"
            ><i class="el-icon-close"></i
          ></span>
        </li>
      </ul>
    </div>
    <div class="tags-next" v-if="arrowVisible" @click="handleClickToRight">
      <i class="el-icon-d-arrow-right" style="color:#ffffff"></i>
    </div>
    <div class="tags-prev" v-if="arrowVisible" @click="handleClickToLeft">
      <i class="el-icon-d-arrow-left" style="color:#ffffff"></i>
    </div>
    <div class="tags-down">
      <el-dropdown @command="handleTags" style="color:#ffffff">
        <i class="el-icon-arrow-down"></i>
        <el-dropdown-menu size="small" slot="dropdown">
          <el-dropdown-item command="other">{{$t("tags.firstlable")}}</el-dropdown-item>
          <el-dropdown-item command="all">{{$t("tags.secondlable")}}</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
import bus from "@/common/eventbus.js"; //传参
import elementResizeDetectorMaker from "element-resize-detector";
export default {
  data() {
    return {
      tagsList: [],
      arrowVisible: false, // 是否显示箭头
      tagsBoxWidth: 0, // ref 为 outBox 的长度
      tagsWidth: 0, // ref 为 tags 的长度
      left: 0,
    };
  },
  methods: {
    isActive(path) {
      return path === this.$route.fullPath;
    },
    // 关闭单个标签
    closeTags(index) {
      const delItem = this.tagsList.splice(index, 1)[0];
      const item = this.tagsList[index]
        ? this.tagsList[index]
        : this.tagsList[index - 1];
      if (item) {
        delItem.path === this.$route.fullPath && this.$router.push(item.path);
      } else {
        this.$router.push("/");
      }
    },
    // 关闭全部标签
    closeAll() {
      this.tagsList = [
        {
          title: "总部大屏",
          path: "/hq",
          name: "hq",
          menuName:'总部大屏'
        }
      ];
      this.$router.push("/hq");
      this.left = 0;
    },
    // 关闭其他标签
    closeOther() {
      const curItem = this.tagsList.filter((item) => {
        return item.path === this.$route.fullPath;
      });
      this.tagsList = curItem;
      this.left = 0;
    },
    // 设置标签
    setTags(route) {
      const isExist = this.tagsList.some((item) => {
        return item.path === route.fullPath;
      });
      if (!isExist) {
        // if (this.tagsList.length >= 20) {
        //   this.tagsList.shift();
        // }
        this.tagsList.push({
          title: route.meta.title,
          path: route.fullPath,
          name: route.name,
          menuName:route.params.menuName||route.meta.title
        });
      }
      bus.$emit("tags", this.tagsList);
    },
    handleTags(command) {
      command === "other" ? this.closeOther() : this.closeAll();
    },
    handleClickToLeft() {
      this.left = 0;
    },
    handleClickToRight() {
      this.left = this.tagsWidth - this.tagsBoxWidth - 124;
    },
  },
  computed: {
    showTags() {
      return this.tagsList.length > 0;
    },
  },
  watch: {
    '$route.meta.title'(newValue, oldValue) {
      this.setTags(this.$route);
    },
  },
  created() {
    this.setTags(this.$route);
    // 监听关闭当前页面的标签页
    bus.$on("close_current_tags", () => {
      for (let i = 0, len = this.tagsList.length; i < len; i++) {
        const item = this.tagsList[i];
        if (item.path === this.$route.fullPath) {
          if (i < len - 1) {
            this.$router.push(this.tagsList[i + 1].path);
          } else if (i > 0) {
            this.$router.push(this.tagsList[i - 1].path);
            console.log('---')
          } else {
            this.$router.push("/");
          }
          this.tagsList.splice(i, 1);
          break;
        }
      }
    });
  },
  mounted() {
    this.len = this.tagsList.length;
    const erd = elementResizeDetectorMaker();
    erd.listenTo(this.$refs.outBox, (ele) => {
      this.$nextTick(() => {
        this.tagsWidth = this.$refs.tags.offsetWidth;
        this.tagsBoxWidth = ele.offsetWidth;
        if (this.tagsBoxWidth >= this.tagsWidth - 40) {
          this.arrowVisible = true;
          this.left = this.tagsWidth - this.tagsBoxWidth - 124;
        } else {
          this.arrowVisible = false;
        }
      });
    });
  },
};
</script>


<style lang="less">
.tags {
  position: relative;
  width: auto;
  height: 30px;
  line-height: 30px;
  overflow: hidden;
  .tags-tab {
    margin: 0;
    height: 30px;
    display: inline-block;
    position: relative;
  }

  ul {
    box-sizing: border-box;
    height: 100%;
    position: relative;
    transition: 0.3s;
    display: flex;
    .tags-li {
      display: flex;
      flex-wrap: nowrap;
      margin: 3px 5px 2px 3px;
      border-radius: 3px;
      font-size: 12px;
      overflow: hidden;
      cursor: pointer;
      height: 23px;
      line-height: 23px;
      // border: 1px solid #e9eaec;
      // background: #fff;
      vertical-align: middle;
      // color: #666;
      -webkit-transition: all 0.3s ease-in;
      -moz-transition: all 0.3s ease-in;
      transition: all 0.3s ease-in;
    }

    // .tags-li:not(.active):hover {
    //   color: #fff;
    //   background: #409eff;
    //   .tags-li-title {
    //     color: #fff;
    //   }
    // }

    // .tags-li.active {
    //   color: #fff;
    //   background: #409eff;
    //   .tags-li-title {
    //     color: #fff;
    //   }
    // }

    .tags-li-title {
      float: left;
      padding: 0 5px 0 12px;
      max-width: 80px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      margin-right: 5px;
      // color: #666;
    }
  }

  .tags-prev {
    position: absolute;
    top: 0;
    left: 0;
    width: 40px;
    height: 30px;
    text-align: center;
    line-height: 30px;
    cursor: pointer;
    font-size: 18px;
    // background: #000000;
    // border: 1px solid #000000;

    // &:hover {
    //   background: gray;
    // }
  }

  .tags-next {
    position: absolute;
    top: 0;
    width: 40px;
    height: 30px;
    text-align: center;
    line-height: 30px;
    cursor: pointer;
    right: 40px;
    font-size: 18px;
    // background: #000000;
    // border: 1px solid #000000;
    // &:hover {
    //   background: gray;
    // }
  }

  .tags-down {
    position: absolute;
    top: 0;
    right: 0;
    width: 40px;
    height: 30px;
    text-align: center;
    line-height: 30px;
    cursor: pointer;
    // background: #000000;
    // border: 1px solid #000000;

    .el-icon-arrow-down {
      width: 40px;
      height: 30px;
    }
  }
}
</style>
